package com.parenting.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.parenting.entities.SystemRoleDO;
import com.parenting.oauth2.config.AuthUtil;
import com.parenting.request.AddRoleRequest;
import com.parenting.request.PageRoleListRequest;
import com.parenting.request.UpdateRoleRequest;
import com.parenting.service.SystemRoleService;
import com.parenting.util.base.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 角色表 前端控制器
 * </p>
 *
 * @author 烟台业达-王世玺
 * @since 2021-09-13
 */
@RestController
@Api(tags = "角色管理", description = "提供角色的增删改查")
@RequestMapping("/role")
public class SystemRoleController {

    @Autowired
    private SystemRoleService systemRoleService;

    /***
     * 角色新增
     * @param role
     */
    @ApiOperation("新增角色")
    @PostMapping
   public   Result saveRole(@RequestBody AddRoleRequest role){
       systemRoleService.saveRole(role);
       return Result.buildSuccess();
   }


    /***
     * 角色修改
     * @param role
     */
    @ApiOperation("修改角色")
    @PutMapping
    public   Result   updateRole(@RequestBody UpdateRoleRequest role){
        systemRoleService.updateRole(role);
        return Result.buildSuccess();
    }

    /****
     * 角色分页列表
     * @param page
     * @return
     */
    @ApiOperation("角色列表")
    @PostMapping("page")
    public Result<IPage<SystemRoleDO>> pageRole (@RequestBody  PageRoleListRequest page){
        IPage<SystemRoleDO> systemRoleDOIPage = systemRoleService.pageRole(page);

        return  Result.buildSuccess(systemRoleDOIPage);
    }

    /***
     * 根据ID查询 角色详情
     * @param roleId
     * @return
     */

    @ApiImplicitParam(name = "roleId", value = "角色ID", required = true)
    @ApiOperation("查询角色详情")
    @GetMapping("{roleId}")
    public Result<SystemRoleDO> getRoleById(@PathVariable("roleId") String roleId){
        SystemRoleDO roleById = systemRoleService.getRoleById(roleId);
        return  Result.buildSuccess(roleById);
    }

    /***
     * 根据 ID集合删除 角色
     * @param roleId
     */
    @ApiImplicitParam(name = "roleId", value = "角色ID", required = true)
    @ApiOperation("删除角色")
    @DeleteMapping("{roleId}")
    public Result deleteRoleById(@PathVariable("roleId") String roleId){
        systemRoleService.deleteRoleById(roleId);
        return  Result.buildSuccess();
    }

    /***
     * 角色下拉
     */
    @ApiOperation("职位下拉")
    @GetMapping
    public   Result<List<SystemRoleDO>> getListRole(){
        List<SystemRoleDO> listRole = systemRoleService.getListRole(AuthUtil.getUserInfo().getShopId());

        return Result.buildSuccess(listRole);
    }
}

